package mastermind.gui.actions;

import java.awt.event.ActionEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.swing.AbstractAction;

import mastermind.gui.Frame;
import mastermind.socket.protocol;
import mastermind.socket.socketConnect;

public class QuitAction extends AbstractAction{

	private Frame frame;
	
	public QuitAction(Frame frame)
	{
		this.frame = frame;
	}
	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		if(frame.getLoginStatus() == true)
		{
			socketConnect connect = frame.connect();
			DataInputStream dio = connect.getInputStream();
			DataOutputStream dos = connect.getOutputStream();
			
			try 
			{
				dos.writeInt(protocol.CLIENT_CLOSING);
			} 
			catch (IOException e) 
			{
				e.printStackTrace();
			}
			frame.closeSocket(); //close sockets before quit if they are open
		}
		System.exit(0); 
	}

}
